<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="params.poleName" @clear="getList" />
      <span class="search-label">一体杆编号：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="params.poleNumber" @clear="getList" />
      <span class="search-label">运行状态：</span>
      <el-select v-model="params.poleStatus">
        <el-option label="正常" :value="0" />
        <el-option label="异常" :value="1" />
        <el-option label="全部" :value="null" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="getList">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="addOpen">添加一体杆</el-button>
      <el-button @click="dltPart">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" :total="total" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column label="一体杆名称" prop="poleName" width="150" />
        <el-table-column label="一体杆编号" prop="poleNumber" width="120" />
        <el-table-column label="一体杆IP" prop="poleIp" width="150" />
        <el-table-column label="安装区域" prop="areaName" width="140" />
        <el-table-column label="一体杆类型" prop="poleType" :formatter="Type" />
        <el-table-column label="运行状态" prop="poleStatus" :formatter="Status" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="dataShow(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="dltPole(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination layout="total, prev, pager, next, sizes, jumper" :total="total" :current-page="params.page"
        :page-size="params.pageSize" :page-sizes="[2, 4, 6, 8, 10]" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <!-- 添加一体杆 -->
    <el-dialog :title="addForm.id ? '编辑一体杆' : '添加一体杆'" width="580px" :visible="flag" top="6vh" @close="handleClose">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="一体杆名称" prop="poleName">
            <el-input v-model="addForm.poleName" />
          </el-form-item>
          <el-form-item label="一体杆编号" prop="poleNumber">
            <el-input v-model="addForm.poleNumber" />
          </el-form-item>
          <el-form-item label="一体杆IP" prop="poleIp">
            <el-input v-model="addForm.poleIp" />
          </el-form-item>
          <el-form-item label="关联区域" prop="areaId">
            <el-select v-model="addForm.areaId" style="width: 100%;">
              <el-option
                v-for="item in areaList"
                :key="item.areaId"
                :value="item.areaId"
                :label="item.areaName"/>
            </el-select>
          </el-form-item>
          <el-form-item label="一体杆类型" prop="poleType" >
            <el-select v-model="addForm.poleType" style="width: 100%;">
              <el-option label="入口" :value="typeEnum.entrance" />
              <el-option label="出口" :value="typeEnum.export" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRodListAPI, addRodAPI, getAreaListAPI, editPoleAPI, dltPoleAPI } from '@/api/rod'
export default {
  data() {
    return {
      list: [],
      total: 0,
      areaList: [],
      typeEnum:{
        entrance: '入口',
        export:'出口'
      },
      params: {
        page: 1,
        pageSize: 8,
        poleName: '',
        poleNumber: '',
        poleStatus: ''
      },
      flag: false,
      addForm: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      },
      addFormRules: {
        poleName: [{ required: true, message: '请输入一体杆名称', trigger: 'blur' }],
        poleNumber: [{ required: true, message: '请输入一体杆编号', trigger: 'blur' }],
        poleIp: [{ required: true, message: '请输入一体杆IP', trigger: 'blur' }],
        areaId: [{ required: true, message: '请选择关联区域', trigger: 'blur' }],
        poleType: [{ required: true, message: '请选择一体杆类型', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getList() {
      const res = await getRodListAPI(this.params)
      //console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    async getAreaList(){
      const res = await getAreaListAPI()
      this.areaList = res.data
    },
    Status(row) {
      return row.poleStatus === 0 ? '正常' : '异常'
    },
    Type(row) {
      return row.poleType === 'export' ? '出口' : '入口'
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.getList()
    },
    handleCurrentChange(page) {
      this.params.page = page
      this.getList()
    },
    addOpen() {
      this.flag = true
    },
    handleClose() {
      this.flag = false
      this.addForm = {
        areaName: '',
        spaceNumber: '',
        areaProportion: '',
        ruleId: '',
        remark: ''
      }
    },
    addPole() {
      addRodAPI(this.addForm).then(()=>{
        this.$message.success('添加成功！')
      }).catch((error)=>{
        this.$message.error(error.response.data.msg)
      })
    },
    dataShow(row){
      this.flag = true
      const { id, poleName, poleNumber, poleIp, areaId, poleType } = row
      this.addForm = { id, poleName, poleNumber, poleIp, areaId, poleType }
    },
    editPole(){
      editPoleAPI(this.addForm).then(()=>{
        this.$message.success('编辑成功！')
      })
    },
    submit() {
      this.$refs.addForm.validate((ok) => {
        if (ok) {
          if(this.addForm.id){
            this.editPole()
          }
          else{
            this.addPole()
          }
          this.getList()
          this.handleClose()
        }
      })
    },
    dltPole(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        dltPoleAPI(id).then(() => {
          this.$message.success('删除成功！')
          this.getList()
        })
      }).catch(()=>{
        this.$message.error('删除失败！')
      })
    },
    handleSelectionChange(rowList){
      const selectedId = rowList.map(item=>item.id).join(',')
      this.selectedId = selectedId
    },
    dltPart(){
      if(this.selectedId){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          dltPoleAPI(this.selectedId).then(()=>{
            this.$message.success('删除成功！')
            this.getList()
          })
        })
      }
      else{
        this.$message.error('请选择要删除的数据！')
      }
    }
  },
  mounted() {
    this.getList()
    this.getAreaList()
  }
}
</script>


<style lang="scss" scoped>
.card-container {
  padding: 5px;
  background-color: #fff;
}


.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
